import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
    ...HttpUtils.func,
    data: {
        iconUrl: "../../img/headICON.png",
        eduArray: ["文盲", "小学", "初中", "高中", "中专", "大专", "本科", "硕士", "博士及以上"],
        eduValues: [1, 2, 3, 4, 5, 6, 7, 8, 9],
        sexArray: ['男', '女'],
        //查询的人员基本信息
        member: {
            userId: 0
        },
        showSubmit: false,
    },
    bindSexChange(e) {
        var index = e.detail.value;
        var sexArr = this.data.sexArray;
        var sex = sexArr[index];
        this.setData({
            "member.sex": sex
        });
    },
    bindPickerChange(e) {
        var index = e.detail.value;
        var eduArray = this.data.eduArray;
        var education = eduArray[index];
        this.setData({
            "member.education": education
        });
    },
    changeIcon(event) {
        //头像不允许修改 
        // wx.chooseImage({
        //   count: 1,   //默认选择照片张数
        //   success: (res) => {
        //     var imeUrl = res.filePaths[0];
        //     console.log("url:"+imeUrl);
        //      this.setData({
        //        iconUrl: res.filePaths[0]
        //     })
        //   },
        // });  
    },
    bindKeyInput(e) {
        this.setData({
            'member.mobile': e.detail.value
        });
    },
    bindBirthdayChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            birthday: e.detail.value
        })
    },
    bindPartyDayChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            partyDay: e.detail.value
        })
    },

    onLoad(params) {
        var userId = params.userId;
        if (userId == null) {
            userId = getApp().globalData.userId;
        }
        this.setData({
            "member.userId": userId,
            "showSubmit": userId == getApp().globalData.userId ? true : false,
        });
        this.loadUserInfo();

    },
    loadUserInfo: function (e) {
        var newUrl = getApp().globalData.dangjianUrl + "/member/memberInfo";
        var params = {};
        params['userId'] = this.data.member.userId + "";
        params = JSON.stringify(params);
        this.httpGet(newUrl, params, "人员基本信息查询失败").then(
            (respData) => {
                var result = respData.data.result.code;
                if (1 == result) {
                    var member = respData.data.member;
                    var sex = member.sex;
                    //性别
                    if (1 == sex) {
                        sex = "男";
                    } else if (2 == sex) {
                        sex = "女";
                    } else {
                        sex = "";
                    }
                    member.sex = sex;
                    //学历
                    var edu = member.education;
                    var eduName = "";
                    var eduValues = this.data.eduValues;
                    for (var index in eduValues) {
                        if (edu == eduValues[index]) {
                            eduName = this.data.eduArray[index];
                            break;
                        }
                    }
                    member.education = eduName;
                    var iconUrl = member.iconUrl;
                    if (null == iconUrl || undefined == iconUrl || "" == iconUrl) {
                        iconUrl = this.data.iconUrl;
                    }
                    //iconUrl="https://static.dingtalk.com/media/lADOAHjfIs0BLM0BLA_300_300.jpg";
                    member.iconUrl = iconUrl;
                    this.setData({
                        "member": member,
                        'partyDay': member.partyDay,
                        'birthday': member.birthday
                    });

                  
                } else {
                    this.showPageMsg("alert", respData.data.result.description)
                }
            }
        );
    },

    updateMemberInfo: function (e) {
        let newUrl = getApp().globalData.dangjianUrl + "/member/updateMemberInfo",
            params = {
                'userId': e.userId,
                'mobile': e.mobile,
                'birthday': e.birthday,
                'partyDay': e.partyDay
            };
        // params['userId'] = e.userId;
        // params['mobile'] = e.mobile;
        // params['birthday'] = e.birthday;
        // params['partyDay'] = e.partyDay;
        // 1-文盲2-小学3-初中4-高中5-中专6-大专7-本科8-硕士9-博士及以上
        var education;
        for (var i in this.data.eduArray) {
            if (this.data.eduArray[i] == e.education) {
                education = parseInt(i) + 1;
            }
        }
        params['education'] = education + "";
        params['sex'] = e.sex == '男' ? '1' : (e.sex == '女' ? '2' : '0'); + "";
        params = JSON.stringify(params);
        this.httpGet(newUrl, params, "成员档案修改接口查询失败").then(
            (respData) => {
                var result = respData.data.result.code;
                if (+result == 1) {
                    this.showPageMsg("tips", "提交成功");
                } else {
                    this.showPageMsg("alert", respData.data.result.description)
                }
            }
        );
    },

    // submitForm: function (e) {
    //     this.updateMemberInfo(e);
    // },

    goUpdata() {
       let data = this.data.member,
           isChekck = this.validateForm(data);

        console.log(isChekck);
        if (isChekck) {
            this.updateMemberInfo(data)
        }
    },
    validateForm: function (fieldValues) {
        var result = false;
        if (null != fieldValues && "" != fieldValues) {
            var userName = fieldValues.userName;
            var sex = fieldValues.sex;
            var education = fieldValues.education;
            var mobile = fieldValues.mobile;
            var birthday = fieldValues.birthday;
            var partyDay = fieldValues.partyDay;
            if (null == userName || '' == userName) {
                this.showPageMsg("tips", "姓名不能为空");
                return result;
            }
            if (null == sex || '' == sex) {
                this.showPageMsg("tips", "性别不能为空");
                return result;
            }
            if (null == education || '' == education) {
                this.showPageMsg("tips", "学历不能为空");
                return result;
            }
            var regex = /^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/;
            if (false == regex.test(mobile)) {
                this.showPageMsg("tips", "手机号码格式不正确");
                return result;
            }

            if (null == birthday || '' == birthday) {
                this.showPageMsg("tips", "出生日期不能为空");
                return result;
            }

            if (null == partyDay || '' == partyDay) {
                this.showPageMsg("tips", "入党日期不能为空");
                return result;
            }
            result = true;
        }
        return result;

    }
});